package cn.qinyun.cloud.common.util;

import cn.qinyun.cloud.common.constant.MateConstant;
import org.slf4j.MDC;

import javax.servlet.http.HttpServletRequest;

/**
 * 类名称：TraceUtil <br>
 * 类描述：TODO <br>
 *
 * @author 张兆发
 * @version 1.0.0
 * @date 创建时间：2021/7/16  11:27 上午 <br>
 */
public class TraceUtil {
    /**
     * 从header和参数中获取traceId
     * 从前端传入数据
     *
     * @param request 　HttpServletRequest
     * @return traceId
     */
    public static String getTraceId(HttpServletRequest request) {
        String traceId = request.getParameter(MateConstant.MATE_TRACE_ID);
        if (StringUtil.isBlank(traceId)) {
            traceId = request.getHeader(MateConstant.MATE_TRACE_ID);
        }
        return traceId;
    }

    /**
     * 传递traceId至MDC
     *
     * @param traceId 　跟踪ID
     */
    public static void mdcTraceId(String traceId) {
        if (StringUtil.isNotBlank(traceId)) {
            MDC.put(MateConstant.LOG_TRACE_ID, traceId);
        }
    }
}
